﻿Imports System.Xml
Imports System.IO
Public Class ConvertControl
    ''' <summary>
    ''' 向外部程序报告转换进度
    ''' </summary>
    ''' <param name="Message">消息文字</param>
    ''' <remarks></remarks>
    Public Event StepCompleted(ByVal Message As String)
    ''' <summary>
    ''' 主程序
    ''' </summary>
    ''' <param name="InputFileName">PL1</param>
    ''' <param name="OutPutFileName">COBOL</param>
    ''' <returns>转换结果</returns>
    ''' <remarks></remarks>
    Function ConvertFile(ByVal InputFileName As String, ByVal OutPutFileName As String) As ConversionResult
        LogWriter = New StreamWriter(MyConfig.LogFileName)
        LogWriter.WriteLine("Start Conversion At :" & Now.ToString)
        LogWriter.WriteLine("InputFileName       :" & InputFileName)
        LogWriter.WriteLine("OutPutFileName      :" & OutPutFileName)
        InitializeConverter()
        If Not IO.File.Exists(InputFileName) Then Return ConversionResult.FileNotFound
        'Try
        '读取所有行
        ReadFileToPL1Source(InputFileName)
        '整理代码格式
        FormatPL1Source()
        '向外部程序报告转换进度
        RaiseEvent StepCompleted("整理代码格式完成")
        '生成语法树
        GenerateSyntaxTree()
        '向外部程序报告转换进度
        RaiseEvent StepCompleted("生成语法树完成")
        '生成COBOL代码
        GenerateCOBOLSource(RootNode, OutPutFileName)
        '向外部程序报告转换进度
        RaiseEvent StepCompleted("生成COBOL代码完成")
        LogWriter.WriteLine("End Conversion At :" & Now.ToString)
        LogWriter.Close()
        RaiseEvent StepCompleted("转换代码完成")
        Return ConversionResult.OK
        'Catch ex As Exception
        '    '异常处理
        '    Dim ErrorWriter As New StreamWriter("", False)
        '    ErrorWriter.Write(ex.ToString)
        '    ErrorWriter.Close()
        '    Return ConversionResult.Exception
        'Finally
        '    LogWriter.Close()
        'End Try
    End Function
    ''' <summary>
    ''' 初始化工具的各个参数
    ''' </summary>
    ''' <remarks></remarks>
    Sub InitializeConverter()
        mXmlDocument = New XmlDocument
        RootNode = mXmlDocument.CreateElement("SourceRoot")
        mIdentificationNode = New IdentificationNode
        mEnvironmentNode = New EnvironmentNode
        mDataNode = New DataNode
        mProcedureNode = New ProcedureNode
        PL1Source = New List(Of String)
        Deepth = 1
        WK_COUNT = 1
        '关键字列表[如果变量名称使用了PL1的非关键字，COBOL关键字的时候，需要替换]
        KeyWordList.Clear()
        KeyWordList.Add("DATE", "DATE-X")
        KeyWordList.Add("TIME", "TIME-X")
        KeyWordList.Add("SPACE", "SPACE-X")
        KeyWordList.Add("ZERO", "ZERO-X")
    End Sub
    ''' <summary>
    ''' 生成语法树
    ''' </summary>
    ''' <remarks></remarks>
    Sub GenerateSyntaxTree()
        '分析指定行
        clsStatementDefine.ProcessLines()
        '调整数据结构
        clsJustifyData.JustifyDataStructure()
        '调整程序结构
        clsJustifyStatement.JustifyProcedureStructure()
        '重置工作变量
        clsJustifyStatement.ResetWorkData()
        RootNode.AppendChild(mIdentificationNode.GetXMLElement)
        RootNode.AppendChild(mEnvironmentNode.GetXMLElement)
        RootNode.AppendChild(mDataNode.GetXMLElement)
        RootNode.AppendChild(mProcedureNode.GetXMLElement)
        mXmlDocument.AppendChild(RootNode)
        If TestMode Then
            Dim XmlSW As IO.StreamWriter = New IO.StreamWriter("C:\ConversionTool\TestData\TMP\T.xml", False, Text.Encoding.GetEncoding("Shift-JIS"))
            mXmlDocument.Save(XmlSW)
            XmlSW.Close()
            'Diagnostics.Process.Start("C:\T.Xml")
        End If
    End Sub
    ''' <summary>
    ''' 生成COBOL代码
    ''' </summary>
    ''' <remarks></remarks>
    Sub GenerateCOBOLSource(ByVal Syntax As XmlElement, ByVal OutPutFileName As String)
        Dim OutputStreamWriter As StreamWriter
        OutputStreamWriter = New IO.StreamWriter(OutPutFileName, False, Text.Encoding.GetEncoding(932))
        For Each synNode As XmlElement In Syntax
            Dim SyntaxNode As clsSyntaxNode = Nothing
            Select Case synNode.Name
                Case "IDENTIFICATION"
                    SyntaxNode = mIdentificationNode
                Case "ENVIRONMENT"
                    SyntaxNode = mEnvironmentNode
                Case "DATA"
                    SyntaxNode = mDataNode
                Case "PROCEDURE"
                    SyntaxNode = mProcedureNode
            End Select
            For Each Str As String In SyntaxNode.Convert2Cobol
                OutputStreamWriter.WriteLine(Str)
            Next
        Next
        OutputStreamWriter.Close()
    End Sub
 End Class